package com.xbongbong.saas.help;

import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisConstant;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 秦春阳
 * "市场管理“ 业务help
 */
@Component
public class MarketManagementBusinessHelp {

    @Resource
    private PackageHelp packageHelp;
    @Resource
    private PaasAppModel paasAppModel;
    @Resource
    private PaasMenuModel paasMenuModel;
    @Resource
    private PaasRedisHelper paasRedisHelper;


    /**
     * 判断套餐版本是否是标准版
     * @param corpid
     * @return
     * @throws XbbException
     * true 标准版  false 旗舰版或者高级版
     */
    public boolean isStandardWithFeeType(String corpid) throws XbbException {
        return packageHelp.isStandardWithFeeType(corpid);
    }

    /**
     * 判断该公司是否启用了"市场管理"应用.
     * @param corpid
     * @return
     */
    public boolean isEnableWithApp(String corpid) throws XbbException {
        String redisKey = corpid + "_" + SystemAppMenuEnum.MARKET_MANAGEMENT.getAlias();
        String value = paasRedisHelper.getValue(RedisPrefixConstant.APP_ENABLE_STATUS, redisKey);
        if(Objects.nonNull(value)){
            //先从缓存中拿
            return Objects.equals(value,"1");
        }else{
            String alias = SystemAppMenuEnum.MARKET_MANAGEMENT.getAlias();
            Boolean isEnabled = paasAppModel.validateAppEnable(null,alias,SaasMarkEnum.SAAS.getCode(), corpid);
            //刷新缓存
            Integer enable = isEnabled ? 1 : 0;
            paasRedisHelper.setValue(RedisPrefixConstant.APP_ENABLE_STATUS, alias,String.valueOf(enable), RedisConstant.HUGE_DURATION);
            return isEnabled;
        }
    }

    /**
     * 判断该公司套餐是“标准版”或者"市场管理"应用是关闭的
     * @param corpid
     * @return
     * @throws XbbException
     */
    public boolean feeTypeIsStandardOrAppIsClosed(String corpid) throws XbbException {
       return isStandardWithFeeType(corpid) || (!isEnableWithApp(corpid));
    }

    /**
     * 判断公司的线索菜单是否关闭. true 关闭；false 开启
     * @return
     */
    public boolean isClosed4ClueMenu(String corpid){
        PaasMenuEntity menuEntity = paasMenuModel.getByAlias(XbbRefTypeEnum.CLUE.getAlias(), corpid);
        if(Objects.isNull(menuEntity) || Objects.equals(menuEntity.getEnable(),0)){
            return true;
        }
        return false;
    }


}
